#include "Iocp.h"

Iocp::Iocp()
	: m_hIocp(INVALID_HANDLE_VALUE)
{
	m_hIocp = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 0);
}


Iocp::~Iocp()
{
	if( m_hIocp != NULL )
		CloseHandle(m_hIocp);
}

void Iocp::Register(HANDLE handle, void *pvCompletionkey)
{
	if( m_hIocp == NULL )
		return;
	::CreateIoCompletionPort(handle, m_hIocp, reinterpret_cast<ulong>(pvCompletionkey), 0);
}

bool Iocp::GetQueuedCompletionStatus(ulong *cbTransferred, PULONG_PTR pvCompletionkey, OVERLAPPED **ppkOverlapped, ulong unWaittime)
{
	if( m_hIocp == NULL )
		return false;
	return ::GetQueuedCompletionStatus(m_hIocp, cbTransferred, pvCompletionkey, ppkOverlapped, unWaittime) == TRUE;
}

bool Iocp::PostQueuedCompletionStatus(ulong cbTransferred, ulong *pvCompletionkey, OVERLAPPED *pkOverlapped)
{
	if( m_hIocp == NULL )
		return false;
	return ::PostQueuedCompletionStatus(m_hIocp, cbTransferred, reinterpret_cast<ulong>(pvCompletionkey), pkOverlapped) == TRUE;
}
